/**
  **************************************************************************
  * @file    config-util.c
  * @author  huhao
  * @version V1.0.0
  * @date    10-20-2020
  * @brief   读取配置文件信息
  * @project linux client
  ***************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2020 上海今中网络科技有限公司 </center></h2>
  ***************************************************************************
*/
#include "error.h"
#include "log-util.h"
#include "config-util.h"

#ifndef init_config
#define init_config  CONFIG_UTIL_INIT
#endif
#ifndef free_config
#define free_config  CONFIG_UTIL_FREE
#endif

/**
  * @brief      去除字符串的首尾空格和回车
  * @param      arg1、待处理字符串
  * @retval     成功返回去除空格的字符串
  */
static char *trim2(char *s)
{
    char *p = s;
    int len = strlen(p);

    while(isspace(p[len - 1]))
		p[--len] = 0;
    while(*p && isspace(*p))
		++p, --len;

    memmove(s, p, len + 1);
    return s;
}


/**
  * @brief      构造函数
  * @param      arg1
  * @retval     void
  */
void init_config(IN CONFIG_UTIL_T *configUtil)
{
    ///初始化对象成员
    //configUtil->initConfig     = init_config;
    configUtil->readConfig     = read_config;
    configUtil->getInt         = get_int;
    configUtil->getString      = get_string;
    configUtil->setInt         = set_int;
    configUtil->setString      = set_string;
    configUtil->commitConfig   = commit_config;
    configUtil->freeConfig     = free_config;
}

/**
  * @brief      读取文件字段
  * @param      arg1、文件名
  * @retval     成功返回0，失败-1
  */
int read_config(IN CONFIG_UTIL_T *configUtil, IN const char *fileName)
{

    int      i                     = 0;
    char    *temp                  = NULL;
    char    *savep                 = NULL;
    char     buffer[CONFIG_MAXLEN] = {0};
    FILE    *fp                    = NULL;

    configUtil->count = 0;
    configUtil->nameList = NULL;
    configUtil->valueList = NULL;
    if ((fp = fopen(fileName, "rt")) == NULL)
    {
//        MSG( LOG_DEBUG, "[read_config] errno %d", errno );
        return -1;
    }


    while (fgets(buffer, CONFIG_MAXLEN , fp))
    {
		if (strlen(buffer) < 2)
			continue;

        temp = trim2(buffer);
        if (temp[0] == '#')
            continue;

        configUtil->count++;
    }
    configUtil->nameList  = (char **)calloc(configUtil->count, sizeof(char *));
    configUtil->valueList = (char **)calloc(configUtil->count, sizeof(char *));
    if (freopen(fileName, "rt", fp) == NULL)
        return -1;
    while (fgets(buffer, CONFIG_MAXLEN , fp))
    {
		if (strlen(buffer) < 2)
			continue;

        temp = trim2(buffer);
        if (temp[0] == '#')
            continue;

        temp = strtok_r(buffer, "=", &savep);
        trim2(temp);
        configUtil->nameList[i] = strdup(temp);

        temp = strtok_r(NULL, "=", &savep);
        temp = strtok_r(temp, "#", &savep);
        trim2(temp);
        configUtil->valueList[i++] = strdup(temp);
    }

	fclose(fp);
	return 0;
}

/**
  * @brief      获取整型数据
  * @param      arg1、field
  * @retval     成功对应数值，失败defaultvalue
  */
int get_int(IN CONFIG_UTIL_T *configUtil, IN const char *field, IN int defaultvalue)
{
    int i = 0;

    for (i=0; i<configUtil->count; i++)
    {
        if (strcmp(field, configUtil->nameList[i]) == 0)
            return atoi(configUtil->valueList[i]);
    }
    return defaultvalue;
}

/**
  * @brief      获取字符串
  * @param      arg1、field
  * @retval     成功对应数值或defaultValue,失败返回NULL
  */
char *get_string(IN CONFIG_UTIL_T *configUtil, IN const char *field, IN char *defaultValue)
{
    int i = 0;

    for (i=0; i<configUtil->count; i++)
    {
        if (strcmp(field, configUtil->nameList[i]) == 0)
            return strdup(configUtil->valueList[i]);
    }

    return strdup(defaultValue);
}

/**
  * @brief      设置整型数据
  * @param      arg1、field arg2、newValue
  * @retval     NULL
  */
void set_int(IN CONFIG_UTIL_T *configUtil, IN const char *field, IN int newValue)
{
    int i = 0;
    char buf[10] = {0};
    snprintf(buf, 9, "%d", newValue);

    for (i=0; i<configUtil->count; i++)
    {
        if (strcmp(field, configUtil->nameList[i]) == 0)
        {
            free(configUtil->valueList[i]);
            configUtil->valueList[i] = strdup(buf);
        }
    }
}

/**
  * @brief      设置字符串数据
  * @param      arg1、field arg2、newValue
  * @retval     NULL
  */
void set_string(IN CONFIG_UTIL_T *configUtil, IN const char *field, IN const char *newValue)
{
    int i = 0;

    for (i=0; i<configUtil->count; i++)
    {
        if (strcmp(field, configUtil->nameList[i]) == 0)
        {
            free(configUtil->valueList[i]);
            configUtil->valueList[i] = strdup(newValue);
        }
    }
}

/**
  * @brief      持久化函数，保存数据到文件(修改文件数据时需调用)
  * @param      arg1、pathName
  * @retval     NULL
  */
void commit_config(IN CONFIG_UTIL_T *configUtil, IN const char *pathName)
{
    int i = 0;
    char buffer[CONFIG_MAXLEN];
    FILE *fp = NULL;

    if ( (fp = fopen(pathName, "wt")) == NULL)
        return;

    for (i=0; i<configUtil->count; i++)
    {
        memset(buffer, 0, CONFIG_MAXLEN);
        sprintf(buffer, "%s = %s\n", configUtil->nameList[i], configUtil->valueList[i]);
        fputs(buffer, fp);
    }

    fflush(fp);
    fclose(fp);
}

/**
  * @brief      析构函数，释放占用的内存(必须调用)
  * @param      arg1、pathName
  * @retval     NULL
  */
void free_config(IN CONFIG_UTIL_T *configUtil)
{
    int i;

    for (i=0; i<configUtil->count; i++)
    {
        free(configUtil->nameList[i]);
        configUtil->nameList[i] = NULL;
        free(configUtil->valueList[i]);
        configUtil->valueList[i] = NULL;
    }

    free(configUtil->nameList);
    configUtil->nameList = NULL;
    free(configUtil->valueList);
    configUtil->valueList = NULL;
    configUtil->count = 0;

    ///null初始化对象成员
    configUtil->readConfig     =  NULL;
    configUtil->getInt         =  NULL;
    configUtil->getString      =  NULL;
    configUtil->setInt         =  NULL;
    configUtil->setString      =  NULL;
    configUtil->commitConfig   =  NULL;

}


/******************* (C) COPYRIGHT 2014 上海今中网络科技有限公司 *****END OF FILE****/














